/**
 * 用户注册
 */
let countDownTime = 0;
$(function() {
	initScrollVentList();
	$('#btnGetCode').click(function() {
		let registerPhone = V('registerPhone');
		if(!phoneNumberIsRight(registerPhone)) {
			$('#phoneErrorMsg').text('请输入正确的手机号码');
			return;
		}
		//60秒倒计时
		countDownTime = 60;
		let btnGetCode = this;
		btnGetCode.disabled = true;
		let checkCodeInterval = window.setInterval(function () {
			countDownTime --;
			if (countDownTime < 1) {
				window.clearInterval(checkCodeInterval);
				$(btnGetCode).css('cursor','pointer').text('获取验证码');
				btnGetCode.disabled = false;
			}else {
				$(btnGetCode).css('cursor','unset').text(countDownTime + '秒后可点击');
			}
		},1000);
		//获取验证码
		getRegisterCheckCode(registerPhone);
	});
	$('#btnRegister').click(function(){
		setButtonStatus(this, true,'注册中...');
		userRegister(this);
	});
	$('#registerAgreement').click(function () {
		$('.btn-group .btn').attr('disabled',!$(this).is(':checked'));
	});
});
/**
 * 获取注册验证码
 */
function getRegisterCheckCode(registerPhone) {
	let errorMsg = $('#phoneErrorMsg'), errMsg = '当前系统繁忙,请稍后再获取验证码';
	$('.error-msg').text('');
	$.ajax({
		type: "POST",
		url: '/ventUserIndex/sendRegisterCheckCode',
		data:{phone: registerPhone},
		dataType: 'json',
		async: false,
		success: function(result) {
			let s = result.status;
			if(s == '1'){
				errMsg = '验证码已发送';
			}else if(s == '2'){
				errMsg = '该号码已注册';
			}else if(s == '4'){
				errMsg = '您当前IP地址获取验证码次数过多，请稍候再试';
			}else if(s == '5'){
				errMsg = '您当前手机号码获取验证码次数过多，请稍候再试';
			}else if(s == '6'){
				errMsg = '您输入的手机号码格式不对，请重新输入';
			}
			if (s != '1'){
				countDownTime = 0;
			}
			errorMsg.text(errMsg);
		},
		error: function() {
			errorMsg.text(errMsg);
		}
	});
}
//用户注册
function userRegister(btnReg){
	let mobileNumber = V('registerPhone'), userPassword = V('registerPwd'),
		regCheckCode = V('checkCode');
	if(!phoneNumberIsRight(mobileNumber)){
		setButtonStatus(btnReg, false,'注册');
		$('.error-msg').text('');
		$('#phoneErrorMsg').text('请输入正确手机号码');
		return;
	}
	if(isNull(regCheckCode) || regCheckCode.length != 6){
		setButtonStatus(btnReg, false,'注册');
		$('.error-msg').text('');
		$('#codeErrorMsg').text('请输入正确的验证码');
		return;
	}
	if(isNull(userPassword) || userPassword.length < 6){
		setButtonStatus(btnReg, false,'注册');
		$('.error-msg').text('');
		$('#pwdErrorMsg').text('请至少输入6位密码');
		return;
	}
	let shaObj = new jsSHA("SHA-1", "TEXT");
	shaObj.update(userPassword);
	let hashPassword = shaObj.getHash("HEX");
	$.ajax({
		type: "POST",
		url: '/ventUserIndex/registerVent',
		data:{phone: mobileNumber, password: hashPassword, sendMsg: regCheckCode},
		dataType: 'json',
		async: false,
		success: function(s) {
			if (s == 1) {
				alert('恭喜您注册成功');
				window.location.href = 'ventLogin.html';
			}else if (s == 2) {
				alert('该手机号码已被注册');
			}else if (s == 3) {
				alert('验证码不正确');
			}else if (s == 4) {
				alert('获取手机号码失败');
			}else if (s == 5) {
				alert('密码不能为空，且不小于6位');
			}else {
				alert('注册失败');
			}
			setButtonStatus(btnReg, false,'注册');
		},
		error: function () {
			alert('系统繁忙，请稍候再试');
			setButtonStatus(btnReg, false,'注册');
		}
	});
}
/**
 * 初始化左边的滚动吐槽数据列表
 */
function initScrollVentList() {
	let scrollSpeed = 30, ventList2 = I('ventList2'),ventList1 = I('ventList1'),
		ventList = I('ventList'),ventUp = I('up'), ventDown = I('down'),scrollHeight = 0,
	downClickNum = 0, downTotalHeight = 0;
	ventList2.innerHTML = ventList1.innerHTML;
	function ventUpScroll(){
		let ventScrollTop = ventList.scrollTop;
		ventList.scrollTop = ventList2.offsetTop <= ventScrollTop ?
			ventScrollTop - ventList1.offsetHeight : ventScrollTop + 1;
		//如果>0，表示只滚动到某个位置后就停止滚动
		if (scrollHeight < 1){
			return;
		}
		scrollHeight ++;
		if (scrollHeight > 110){
			scrollHeight = 0;
			clearScroll();
		}
	}
	let clearScroll = function() {
		clearInterval(ventScrollInterval);
	},
	startScroll = function() {
		scrollSpeed = 30;
		//鼠标移开时，scrollHeight = 0 表示不控制滚动高度，即可无限循环滚动
		scrollHeight = 0;
		downTotalHeight = 0;
		clearScroll();
		downClickNum = 0;
		ventScrollInterval = setInterval(ventUpScroll,scrollSpeed);
	};
	let ventScrollInterval = setInterval(ventUpScroll,scrollSpeed);
	//鼠标移上去，清除滚动事件
	ventList.onmouseover = function () {
		clearScroll();
		$('.up-down').show();
	};
	ventUp.onmouseover = clearScroll;
	ventDown.onmouseover = clearScroll;
	//鼠标移开，启动滚动事件
	ventList.onmouseout = function () {
		startScroll();
		$('.up-down').hide();
	};;
	ventUp.onmouseout = startScroll;
	ventDown.onmouseout = startScroll;

	$('#up').click(function () {
		//scrollHeight 设置 > 0 表示只滚动到某一个高度
		scrollHeight = 1;
		//加快滚动速度
		scrollSpeed = scrollSpeed / 2;
		clearScroll();
		//启动滚动事件
		ventScrollInterval = setInterval(ventUpScroll,scrollSpeed);
	});

	function ventDownScroll(){
		ventList.scrollTop --;
		if (downTotalHeight < 1){
			return;
		}
		downTotalHeight --;
		if (downTotalHeight > 0){
			return;
		}
		if (downClickNum < 1){
			downTotalHeight = 0;
			clearScroll();
		} else {
			downTotalHeight = 1;
			downClickNum --;
		}
	}
	$('#down').click(function () {
		let needTop = ventList.scrollTop;
		if (needTop <= 110){
			ventList.scrollTop = ventList1.offsetHeight - needTop;
		}
		downTotalHeight = (downClickNum + 1) * 110;
		//加快滚动速度
		scrollSpeed = scrollSpeed / 2;
		//先清除原来的滚动，再启动新的滚动事件
		clearScroll();
		ventScrollInterval = setInterval(ventDownScroll,scrollSpeed);
		downClickNum ++;
	});
}